import java.awt.event.ActionListener;

public class JavaKE4 {
    private String outerField="外部类字段";
    private static String staticOuterField="静态外部类字段";

    //成员内部类
    class InnerClass{
        private String innerField="内部类字段";

        public void display(){
            System.out.println("访问外部类实例字段"+outerField);
            System.out.println("访问外部类静态字段"+staticOuterField);
            System.out.println("访问内部类字段"+innerField);
        }
    }

    //静态内部类
    static class StaticInnerClass{
        public void display(){
            System.out.println("只能访问外部静态字段"+staticOuterField);
            //System.out.println(outerField);
        }
    }

    //方法内部类
    public void methodWithLocalClass(){
        final String localVar="方法局部变量";

        class LocalClass{
            public void display(){
                System.out.println("访问方法局部变量"+localVar);
                System.out.println("访问外部类变量"+outerField);
            }
        }
        LocalClass local=new LocalClass();
        local.display();
    }

    //匿名内部类-接口
    public void methodWithAnonymousClass(){
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                System.out.println("匿名内部类执行");
                System.out.println("访问外部类字段" + outerField);
            }
        };
        new Thread(runnable).start();
    }
    /*
    继承
    Thread t=new Thread(){
        @Override
        public void run(){
            System.out.println("匿名内部类继承Thread类");
        }
    };
    作为方法参数
    button.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            System.out.println("按钮被点击");
        }
    });
*/
    public static void main(String[] args){
        JavaKE4 outer=new JavaKE4();

        InnerClass inner=outer.new InnerClass();
        inner.display();

        StaticInnerClass staticInner=new StaticInnerClass();
        staticInner.display();

        outer.methodWithLocalClass();

        outer.methodWithAnonymousClass();
    }
}
